package com.ruoyi.background.service.impl;

import com.ruoyi.background.domain.AppGradeLevel;
import com.ruoyi.background.mapper.AppGradeLevelMapper;
import com.ruoyi.background.service.IAppGradeLevelService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 积分等级Service业务层处理
 *
 * @author suus
 * @date 2024-08-25
 */
@Service
public class AppGradeLevelServiceImpl implements IAppGradeLevelService {
    @Autowired
    private AppGradeLevelMapper appGradeLevelMapper;

    /**
     * 查询积分等级
     *
     * @param id 积分等级主键
     * @return 积分等级
     */
    @Override
    public AppGradeLevel selectAppGradeLevelById(Long id) {
        return appGradeLevelMapper.selectAppGradeLevelById(id);
    }

    /**
     * 获取下一等级
     *
     * @param grade 分数
     * @return 结果
     */
    @Override
    public AppGradeLevel getNextAppGradeLevel(Integer grade) {
        return appGradeLevelMapper.getNextAppGradeLevel(grade);
    }

    /**
     * 获取上一等级
     *
     * @param grade 分数
     * @return 结果
     */
    @Override
    public AppGradeLevel getBeforeAppGradeLevel(Integer grade) {
        return appGradeLevelMapper.getBeforeAppGradeLevel(grade);
    }

    /**
     * 查询积分等级列表
     *
     * @param appGradeLevel 积分等级
     * @return 积分等级
     */
    @Override
    public List<AppGradeLevel> selectAppGradeLevelList(AppGradeLevel appGradeLevel) {
        return appGradeLevelMapper.selectAppGradeLevelList(appGradeLevel);
    }

    /**
     * 新增积分等级
     *
     * @param appGradeLevel 积分等级
     * @return 结果
     */
    @Override
    public int insertAppGradeLevel(AppGradeLevel appGradeLevel) {
        appGradeLevel.setCreateTime(DateUtils.getNowDate());
        appGradeLevel.setCreateBy(SecurityUtils.getUsername());
        appGradeLevel.setUpdateTime(DateUtils.getNowDate());
        appGradeLevel.setUpdateBy(SecurityUtils.getUsername());
        return appGradeLevelMapper.insertAppGradeLevel(appGradeLevel);
    }

    /**
     * 修改积分等级
     *
     * @param appGradeLevel 积分等级
     * @return 结果
     */
    @Override
    public int updateAppGradeLevel(AppGradeLevel appGradeLevel) {
        AppGradeLevel level = appGradeLevelMapper.selectAppGradeLevelById(appGradeLevel.getId());
        if (ObjectUtils.isEmpty(level)) throw new RuntimeException("对象不存在！");
        if (1 == level.getDelFlag()) throw new RuntimeException("对象已删除！");
        appGradeLevel.setUpdateTime(DateUtils.getNowDate());
        appGradeLevel.setUpdateBy(SecurityUtils.getUsername());
        return appGradeLevelMapper.updateAppGradeLevel(appGradeLevel);
    }

    /**
     * 修改状态
     *
     * @param id   id
     * @param type 状态
     * @return 结果
     */
    @Override
    public int changeGradeLevelStatus(Long id, Integer type) {
        AppGradeLevel level = appGradeLevelMapper.selectAppGradeLevelById(id);
        if (ObjectUtils.isEmpty(level)) throw new RuntimeException("对象不存在！");
        if (1 == level.getDelFlag()) throw new RuntimeException("对象已删除！");
        AppGradeLevel gradeLevel = new AppGradeLevel();
        gradeLevel.setId(level.getId());
        gradeLevel.setStatus(type);
        gradeLevel.setUpdateTime(DateUtils.getNowDate());
        gradeLevel.setUpdateBy(SecurityUtils.getUsername());
        return appGradeLevelMapper.updateAppGradeLevel(gradeLevel);
    }

    /**
     * 批量删除积分等级
     *
     * @param ids 需要删除的积分等级主键
     * @return 结果
     */
    @Override
    public int deleteAppGradeLevelByIds(Long[] ids) {
        return appGradeLevelMapper.deleteAppGradeLevelByIds(ids);
    }

    /**
     * 删除积分等级信息
     *
     * @param id 积分等级主键
     * @return 结果
     */
    @Override
    public int deleteAppGradeLevelById(Long id) {
        return appGradeLevelMapper.deleteAppGradeLevelById(id);
    }
}
